package com.jmpt.fangbohui.helper;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.MyApplication;
import com.jmpt.fangbohui.bean.UserBean;

/**
 * 
 * @package com.jmpt.fangbohui.helper
 * @author Victor 
 * @description 用户相关帮助类
 *
 */
public class AccountHelper {
    public static final String ACTION_LOGIN = "com.jmpt.fangbohui.action.login";
    public static final String ACTION_LOGOUT = "com.jmpt.fangbohui.action.logout";
    public static final String ACTION_USER_INFORMATION_CHANGE = "com.jmpt.fangbohui.action.user_information_change";
    /**
     * 通过配置文件保存的用户信息获取已经登陆的用户对象
     * @return
     */
    public static UserBean getUser() {
        String s = MyApplication.getSharedPreferences().getString(Constants.SharedPreferences.USER, null);
        if (TextUtils.isEmpty(s)) {
            return null;
        } else {
            return new Gson().fromJson(s, UserBean.class);
        }
    }
    /**
     * 将当前登陆的用户保存到配置文件中
     * @param user
     */
    public static void setUser(UserBean user) {
        if (TextUtils.isEmpty(user.token)) {
            UserBean currentUser = getUser();
            if (currentUser != null) {
                user.token = currentUser.token;
            }
        }
        SharedPreferences.Editor editor = MyApplication.getSharedPreferences().edit();
        editor.putString(Constants.SharedPreferences.USER, new Gson().toJson(user));

        editor.commit();
        editor.clear();
    }
    /**
     * 用户是否已经登陆
     * @return
     */
    public static boolean isLogin() {
        UserBean user = getUser();
        if (user == null) {
            return false;
        } else {
            return true;
        }
    }
    /**
     * 判断用户是否已经登陆  如果没有登陆 跳到登陆页面
     * @param context
     * @return
     */
    public static boolean isLoginAndOpenLogin(Context context) {
        UserBean user = getUser();
        if (user == null) {
//            Intent intent = new Intent(context, LogintActivity.class);
//            context.startActivity(intent);
            return false;
        } else {
            return true;
        }
    }
    /**
     * 发送一个用户登录成功的广播
     * @param context
     */
    public static void sendLoginBroadCast(Context context) {
        Intent intent = new Intent(ACTION_LOGIN);
        context.sendBroadcast(intent);
    }
    /**
     * 发送一个用户注销的广播
     * @param context
     */
    public static void Logout(Context context) {
        SharedPreferences.Editor editor = MyApplication.getSharedPreferences().edit();
        editor.putString(Constants.SharedPreferences.USER, null);
        editor.putString(Constants.SharedPreferences.HISTORY, "");
        editor.commit();
        editor.clear();
        Intent intent = new Intent(ACTION_LOGOUT);
        context.sendBroadcast(intent);
    }
    /**
     * 发送一个更改登录用户的广播
     * @param context
     */
    public static void sendUserChangeBroadcast(Context context) {
        Intent intent = new Intent(ACTION_USER_INFORMATION_CHANGE);
        context.sendBroadcast(intent);
    }
}
